package org.imis.action;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.hibernate.HibernateException;
import org.imis.domain.Teach;
import org.imis.service.NewsManage;

import com.opensymphony.xwork2.ActionSupport;

public class SaveTeachAlter extends ActionSupport{
	private NewsManage newsManage;
	private String id;
	private String name;
	private String introduce;
   
	private File upload;//动作类上传的属性必须是file类型,upload为表单的name值  
    private String uploadFileName; //上传的文件名称,固定写法:name+FileName;  
    private String uploadContentType; //上传文件的mime类型,固定写法: name+ContextType;  
    //mime:设定某种扩展名的文件用一种应用程序来打开的方式类型，当该扩展名文件被访问的时候，浏览器会自动使用指定应用程序来打开  
    public String execute() throws NumberFormatException, HibernateException, ClassNotFoundException {  
        try {  
        	 Date date=new Date();  
    		 SimpleDateFormat formatter=new SimpleDateFormat("yyyyMMddHHmmss");  
    		 String time=formatter.format(date);
    		 int number = new Random().nextInt(100000) + 1000;
    		 String picurl = time+String.valueOf(number);
    				 //我们需要做的就是提供一个服务器的存放地址即可  
            String realPath = ServletActionContext.getServletContext().getRealPath("/upload");  
            System.out.println(realPath+"			"+picurl+"				"+uploadFileName+name+"      " +introduce);
            //判断路径是否存在,不存在创建  
            File dir = new File(realPath);  
            if (!dir.exists()) {  
                dir.mkdirs();  
            }  
            //第一个参数是文件 ,第二个参数是文件在服务器中的位置,fileUtils 是org.apache.commons.io.FileUtils提供好的现成的方法  
            //FileUtils.copyFile(upload,new File(dir,uploadFileName));//copy文件,服务器中有备份文件  
            FileUtils.moveFile(upload,new File(dir,picurl));//剪切文件,推荐使用,无备份文件  
            Teach teach = (Teach)newsManage.findOneList("Teach",Integer.parseInt(id));
            File oldFile = new File(teach.getPicurl());
            oldFile.delete();
            teach.setName(name);
            teach.setIntroduce(introduce);
            teach.setPicurl("./upload/"+picurl);
            newsManage.saveAlter(teach);
        } catch (IOException e) {  
            e.printStackTrace();  
            return "error";  
        }  
        
        return "success";  
    }
    public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public NewsManage getNewsManage() {
		return newsManage;
	}
	public void setNewsManage(NewsManage newsManage) {
		this.newsManage = newsManage;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getIntroduce() {
		return introduce;
	}
	public void setIntroduce(String introduce) {
		this.introduce = introduce;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}  
}
